﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace OMSService
{
    public class OrderItemManager
    {
        private static OrderItem GetOrderItemById(int id)
        {
            OrderItem orderItem;
            using (OMSModelContainer context = new OMSModelContainer())
            {
                orderItem = (from m in context.OrderItems where m.Id == id select m).First();
            }
            return orderItem;
        }

        public static OrderItemModel New(int quantity, decimal price, string dimension, ItemModel itemModel)
        {
            OrderItemModel OrderItemModel = new OrderItemModel(quantity, price, dimension, itemModel);
            return OrderItemModel;
        }

        public static bool Save(OrderItemModel itemModel)
        {
            try
            {
                using (OMSModelContainer context = new OMSModelContainer())
                {
                    OrderItem orderItem = new OrderItem
                    {
                        Dimension = itemModel.Dimension,
                        Price = itemModel.Price,
                        Quantity = itemModel.Quantity
                    };
                    context.OrderItems.AddObject(orderItem);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public static bool Create(int quantity, decimal price, string dimension, ItemModel itemModel)
        {
            OrderItemModel item = New(quantity, price, dimension, itemModel);
            return (Save(item));
        }

        public static OrderItemModel GetById(int id)
        {
            OrderItem orderItem;
            OrderItemModel orderItemModel;
            using (OMSModelContainer context = new OMSModelContainer())
            {
                orderItem = (from m in context.OrderItems where m.Id == id select m).FirstOrDefault();
                orderItemModel = new OrderItemModel(orderItem);
            }
            return orderItemModel;
        }

        public static List<OrderItemModel> GetAll()
        {
            List<OrderItem> items;
            using (OMSModelContainer context = new OMSModelContainer())
            {
                items = context.OrderItems.ToList();
            }
            List<OrderItemModel> tmpItems = new List<OrderItemModel>();
            foreach (OrderItem i in items)
            {
                tmpItems.Add(new OrderItemModel(i));
            }
            return tmpItems;
        }

        public static bool Update(OrderItemModel itemModel)
        {
            try
            {
                using (OMSModelContainer context = new OMSModelContainer())
                {
                    OrderItem orderItem = (from m in context.OrderItems where m.Id == itemModel.Id select m).First();
                    Order order = context.Orders.Where(m => m.Id == orderItem.OrderId).FirstOrDefault();
                    order.TotalPrice -= orderItem.Price;
                    orderItem.Price = itemModel.Price;
                    order.TotalPrice += orderItem.Price;
                    orderItem.Quantity = itemModel.Quantity;
                    orderItem.Dimension = itemModel.Dimension;
                    context.OrderItems.ApplyCurrentValues(orderItem);
                    context.Orders.ApplyCurrentValues(order);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public static bool Delete(int id)
        {
            try
            {
                using (OMSModelContainer context = new OMSModelContainer())
                {
                    OrderItem orderItem = (from m in context.OrderItems where m.Id == id select m).First();
                    Order order = context.Orders.Where(m => m.Id == orderItem.OrderId).FirstOrDefault();
                    order.TotalPrice -= orderItem.Price;
                    Item item = orderItem.Item;
                    item.Quantity = orderItem.Quantity;
                    context.Items.ApplyCurrentValues(item);
                    context.OrderItems.DeleteObject(orderItem);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }


    }
}